! get grid element data at one time 
function  write_to_db(data,datalen,longitude,latitude,layer,time,year,type)
  implicit none
  integer :: longitude
  integer :: latitude
  integer :: layer
  integer :: time
  integer :: datalen
  integer :: write_to_db
  integer :: si
  integer :: index
  integer :: i,j,k,m,m2,k2
  integer :: iret
  integer :: year
  integer :: type
  integer :: day2db
  integer :: month2db
  character(len=32) :: kind
  character(len=32) :: element
  !real, pointer :: variable(:,:,:)
  real,dimension(datalen) :: data
  !real,dimension(longitude,latitude,layer,time) :: data_todb
  real,allocatable:: data_todb(:,:,:,:)
  allocate(data_todb(longitude,latitude,layer,time))
  kind = 'java'
  element= 'element'
  si = size(data)
   print *,'in the fortran'
  !print *,data
 
  index =1
  do m=1,time
     do k=1,layer
	    do j=1,latitude
		  do i=1,longitude
		     data_todb(i,j,k,m) = data(index)
			 index=index+1
		  enddo
		enddo
	 enddo
  enddo
  if(type==1)then
    iret = day2db(data_todb,year,longitude,latitude,layer,time,kind,element)
  endif
  if(type==2)then
   iret =  month2db(data_todb,year,longitude,latitude,layer,time,kind,element)
  endif
  deallocate(data_todb);
  print *,si
  print *,longitude
  print *,latitude
  print *,layer
  print *,time
  print *,year
  print *,type
  write_to_db = 0
  return
end

subroutine  day2date(year,yearday,month_int,day,iret)
    implicit none
    integer,intent(in) ::year
    integer,intent(in) ::yearday
    integer,intent(out) :: month_int
    integer,intent(out) ::day
    integer,intent(out) ::iret
    integer :: i
 integer, parameter :: month(12) = [31,28,31,30,31,30,31,31,30,31,30,31]
 integer, parameter :: tenday(36) = [ 10, 10, 11, 10, 10,  8, 10, 10, &
      11, 10, 10, 10, 10, 10, 11, 10, 10, 10, 10, 10, 11, 10, 10, 11, &
      10, 10, 10, 10, 10, 11, 10, 10, 10, 10, 10, 11]
 integer, parameter :: pentad(72) = [ 5,  5,  5,  5,  5,  6,  5,  5,  5,  &
      5,  5,  3,  5,  5,  5,  5,  5,  6,  5,  5,  5,  5,  5,  5,  5,  5,  &
      5,  5,  5,  6,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  5,  &
      5,  5,  5,  5,  6,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  &
      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6]

 integer, parameter :: month_offset(12) = [ 0, 31, 59, 90, 120, 151, 181, &
                                            212, 243, 273, 304, 334]
 integer, parameter :: tenday_offset(36) = [ 0, 10, 20, 31, 41, 51, 59, 69, &
         79, 90,100,110,120,130,140,151,161,171,181,191,201,212,222,232,243,&
         253,263,273,283,293,304,314,324,334,344,354]
 integer, parameter :: pentad_offset(72) = [ 0,  5, 10, 15, 20, 25, 31, 36, &
        41, 46, 51, 56, 59, 64, 69, 74, 79, 84, 90, 95,100,105,110,115,120, &
        125,130,135,140,145,151,156,161,166,171,176,181,186,191,196,201,206,&
        212,217,222,227,232,237,243,248,253,258,263,268,273,278,283,288,293,&
        298,304,309,314,319,324,329,334,339,344,349,354,359]

! leap Year 
 integer, parameter :: month_leap(12) = [31,29,31,30,31,30,31,31,30,31,30,31]
 integer, parameter :: tenday_leap(36) = [ 10, 10, 11, 10, 10,  9, 10, 10, &
      11, 10, 10, 10, 10, 10, 11, 10, 10, 10, 10, 10, 11, 10, 10, 11, 10, &
      10, 10, 10, 10, 11, 10, 10, 10, 10, 10, 11]
 integer, parameter :: pentad_leap(72) = [ 5,  5,  5,  5,  5,  6,  5,  5, &
       5,  5,  5,  4,  5,  5,  5,  5,  5,  6,  5,  5,  5,  5,  5,  5,  5, &
       5,  5,  5,  5,  6,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6, &
       5,  5,  5,  5,  5,  6,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, &
       6,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6]

 integer, parameter :: month_leap_offset(12) = [ 0, 31, 60, 91, 121, 152, &
                                                 182, 213, 244, 274, 305, 335]
 integer, parameter :: tenday_leap_offset(36) = [ 0, 10, 20, 31, 41, 51, 60, &
     70, 80, 91,101,111,121,131,141,152,162,172,182,192,202,213,223,233,244, &
     254,264,274,284,294,305,315,325,335,345,355]
 integer, parameter :: pentad_leap_offset(72) = [ 0,  5, 10, 15, 20, 25, 31, &
     36, 41, 46, 51, 56, 60, 65, 70, 75, 80, 85, 91, 96,101,106,111,116,121, &
     126,131,136,141,146,152,157,162,167,172,177,182,187,192,197,202,207,213,&
     218,223,228,233,238,244,249,254,259,264,269,274,279,284,289,294,299,305,&
     310,315,320,325,330,335,340,345,350,355,360]
    do i=1,12
	   if(i<12)then
	      if(yearday>month_leap_offset(i).AND.yearday<=month_leap_offset(i+1))then
			month_int =i
		    day =yearday-month_leap_offset(i)
			! print *,'********************'
			! print *,yearday
			! print *,month_int
			! print *,day
			return
	     endif
		else 
		  month_int =12
		  day =yearday-month_leap_offset(12)
		  !print *,'********************'
			! print *,yearday
			! print *,month_int
			! print *,day
		  return
	   endif
	enddo
	!month_int =4
	!day = yearday
	!iret =1
	!return
end subroutine day2date